package com.yzt.minemodule.fragment;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yzt.zhuju.base.BaseFragment;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.mine.GiftListBean;
import com.yzt.zhuju.network.entity.user.PersonalDetail;
import com.yzt.zhuju.network.entity.user.PersonalInfoEntry;
import com.yzt.zhuju.utils.ViewUtil;
import com.yzt.zhuju.utils.recyclerutils.GridItemDecoration;
import com.yzt.minemodule.R;
import com.yzt.minemodule.adapter.PersonalGiftNewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * author : bob
 * date   : 2022/12/7
 * desc   : 个人资料子页面
 */
@SuppressLint("SetTextI18n")
public class PersonalInfoFragment extends BaseFragment {

    private String mUserId;

    private PersonalGiftNewAdapter mPersonalGiftAdapter;

    /**
     * 用户基础信息
     */
    private PersonalDetail mPersonalDetail;

    /**
     * 用户信息列表
     */
    private List<PersonalInfoEntry> mInfoList = new ArrayList<>();

    /**
     * 已点亮数量
     */
    private TextView tvGiftLight;

    public static PersonalInfoFragment newInstance(String mUserId) {
        PersonalInfoFragment infoFragment = new PersonalInfoFragment();
        infoFragment.mUserId = mUserId;
        return infoFragment;
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_personal_info;
    }

    @Override
    public void installViews() {

        // 礼物列表
        loadGiftRecycle();

        // 获取礼物
        loadGiftData();
    }

    /**
     * 礼物列表
     */
    private void loadGiftRecycle() {
        tvGiftLight = findViewById(R.id.tv_gift_light);
        tvGiftLight.setOnClickListener(this);
        TextView mPersonalGiftTv = findViewById(R.id.personal_gift_label);
        mPersonalGiftTv.setOnClickListener(this);
        RecyclerView mPersonalGiftRv = findViewById(R.id.personal_gift_rv);
        mPersonalGiftRv.addItemDecoration(new GridItemDecoration.Builder()
                .divider(24)
                .build());

        mPersonalGiftAdapter = new PersonalGiftNewAdapter();
        if (isAdded()) {
            mPersonalGiftRv.setLayoutManager(new GridLayoutManager(mActivity, 4));
        }
        mPersonalGiftRv.setAdapter(mPersonalGiftAdapter);
    }

    @Override
    public void netWorkRequest() {

    }

    @Override
    public void onClick(View view) {
        super.onClick(view);

        if (mUserId == null || mPersonalDetail == null) {
            return;
        }

        if (ViewUtil.isFastDoubleClick()) {
            return;
        }

        if (view.getId() == R.id.personal_gift_label || view.getId() == R.id.tv_gift_light) {
            // 收到礼物
         //   GiftWallActivity.start(getActivity(), mUserId, mPersonalDetail.getUserBasic().getNickName(), mPersonalDetail.getUserBasic().getAvatar());
        }
    }

    /**
     * 加载用户信息
     */
    public void initData(PersonalDetail personalDetail) {
        mPersonalDetail = personalDetail;
    }

    /**
     * 获取礼物列表
     */
    public void loadGiftData() {

        BizLayer.getInstance().getAccountModule().loadGiftWallList(mUserId, mCurrentPage, 8, new AresSubscriber<GiftListBean>() {

            @Override
            protected void onSuccess(GiftListBean giftListEneity) {

                if (giftListEneity == null) {
                    return;
                }

                tvGiftLight.setText("（" + getString(R.string.is_light) + giftListEneity.getLightNum() + "/" + giftListEneity.getAllGiftNum() + "）");
                if (giftListEneity.getGiftList() != null) {
                    mPersonalGiftAdapter.setList(giftListEneity.getGiftList());
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mInfoList != null) {
            mInfoList.clear();
        }
        mInfoList = null;
    }
}